﻿@using vueweb.Controllers;
@using System.Collections.Generic;
@using System.Linq;
@using System.Text.RegularExpressions;
@{
	Dictionary<string, string> svgfiles =
		NodeJsController.Get文件名称和文件内容("vue/icons/svg", false, "svg");
	List<string> keys = svgfiles.Keys.ToList();
	List<string> ids = keys.Select(k => {
		return "icon-" + System.IO.Path.GetFileNameWithoutExtension(k);
	}).ToList();
	Regex w = new Regex("width\\s*=\\s*[\"'](\\d+)[\"']",RegexOptions.IgnoreCase|RegexOptions.Multiline);
	Regex h = new Regex("height\\s*=\\s*[\"'](\\d+)[\"']",RegexOptions.IgnoreCase | RegexOptions.Multiline);
	Regex id = new Regex("id\\s*=\\s*[\"']^[\"']*[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
	Regex svg = new Regex("<\\s*svg", RegexOptions.IgnoreCase | RegexOptions.Multiline);
	Regex svg2 = new Regex("<\\s*/\\s*svg\\s*>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
	Regex viewbox = new Regex("viewbox\\s*=\\s*[\"'][^\"^']*[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
<svg xmlns="http://www.w3.org/2000/svg"
	 xmlns:xlink="http://www.w3.org/1999/xlink"
	 style="position: absolute; width: 0; height: 0"
	 id="__SVG_SPRITE_NODE__">
	@for (int i = 0; i < keys.Count; i++)
	{
		string content = svgfiles[keys[i]];
		string content2 = id.Replace(content, "");
		content2 = svg.Replace(content2, "<symbol " + $"id=\"{ids[i]}\" ");
		content2 = svg2.Replace(content2, "</symbol>");
		if (!viewbox.IsMatch(content2)) {
			string width = w.Match(content).Groups[1].Value;
			string height = h.Match(content).Groups[1].Value;
			string viewboxStr = $" viewBox=\"0 0 {width} {height}\" ";
			content2 = content2.Replace("<symbol ", "<symbol " + viewboxStr);
		}		
		content2 = w.Replace(content2, "");
		content2 = h.Replace(content2, "");
		@Html.Raw(content2)
	}	
</svg>
